function [m,b,r,p] = linreg(xi,yi)
% linear regression on x and y
% results include:
% y=mx+b
% r is correlation coeff.
% p is the p value for the fit.
% [m,b,r,p] = linreg(x,y)
%
% 12/1/99 P. Manis
m=NaN;
b=NaN;
r=[];
p=[];
dxi = size(xi);
dyi = size(yi);
if(prod(dxi) ~= prod(dyi))
    fprintf(1, 'linred: xi and yi not same size\n');
    return;
end;
[md, j] = max(dxi);
[md, k] = max(dyi);
if(j ~= k)
    yi = yi';
end;

x=xi(find(~isnan(xi) & ~isnan(yi)));
y=yi(find(~isnan(yi) & ~isnan(xi)));
if(length(x) < 2)
	disp('Cannot fit line to less than 2 points')
	return;
end;
[m n]=size(x);
if(n > m)
	x=x';
end;
[m n] = size(y);
if(n > m)
	y=y';
end;
[r, p, z, df] = pearson(x',y'); % get the statistics
E=[ones(length(x),1) x]; % make the basis function
c=E\y; % minimization/least squares...
b=c(1);
m=c(2);
return;

